/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.entity.DTO.MonthInitDTO;
import com.jiadakeji.ppm.modules.business.entity.FeeMonth;
import com.jiadakeji.ppm.modules.business.service.FeeMonthService;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 缴费月份Controller
 * @author HHAOYU
 * @version 2019-07-09
 */
@Controller
@RequestMapping(value = "${adminPath}/business/feeMonth")
public class FeeMonthController extends BaseController {

	@Autowired
	private FeeMonthService feeMonthService;
	
	@ModelAttribute
	public FeeMonth get(@RequestParam(required=false) String id) {
		FeeMonth entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = feeMonthService.get(id);
		}
		if (entity == null){
			entity = new FeeMonth();
		}
		return entity;
	}
	
	@RequiresPermissions("business:feeMonth:view")
	@RequestMapping(value = {"list", ""})
	public String list(FeeMonth feeMonth, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FeeMonth> page = feeMonthService.findPage(new Page<FeeMonth>(request, response), feeMonth); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("feeMonth", feeMonth);
		return "modules/business/feeMonthList";
	}

	@RequiresPermissions("business:feeMonth:view")
	@RequestMapping(value = "form")
	public String form(FeeMonth feeMonth, Model model) {
		model.addAttribute("feeMonth", feeMonth);
		return "modules/business/feeMonthForm";
	}

	@RequiresPermissions("business:feeMonth:edit")
	@RequestMapping(value = "save")
	public String save(FeeMonth feeMonth, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, feeMonth)){
			return form(feeMonth, model);
		}
		feeMonthService.save(feeMonth);
		addMessage(redirectAttributes, "保存缴费月份成功");
		
		redirectAttributes.addFlashAttribute("feeMonth", UserUtils.getCache("feeMonth"));
		return "redirect:"+Global.getAdminPath()+"/business/feeMonth/?repage";
	}
	
	@RequiresPermissions("business:feeMonth:edit")
	@RequestMapping(value = "delete")
	public String delete(FeeMonth feeMonth, RedirectAttributes redirectAttributes) {
		feeMonthService.delete(feeMonth);
		addMessage(redirectAttributes, "删除缴费月份成功");
		
		redirectAttributes.addFlashAttribute("feeMonth", UserUtils.getCache("feeMonth"));
		return "redirect:"+Global.getAdminPath()+"/business/feeMonth/?repage";
	}



	@RequestMapping("preInit")
	@RequiresPermissions("business:feeMonth:edit")
	public String preInit(@Valid Model model, MonthInitDTO monthInitDTO){
		monthInitDTO = feeMonthService.preMontinit();
		model.addAttribute("monthInitDTO", monthInitDTO);
		return "modules/business/monthInitForm";
	}

	// TODO: 2017/11/1  初始化成功 给页面显示初始化 多少个电表，多少个水表
	@RequestMapping("init")
	@RequiresPermissions("business:feeMonth:edit")
	public String init(MonthInitDTO monthInit,Model model,RedirectAttributes redirectAttributes){
		String message = "初始化月份失败";
		model.addAttribute("monthInit", monthInit);
		if(feeMonthService.validMonthInit(monthInit)){
			UserUtils.removeCache(UserUtils.CACHE_FEEMONTH_LIST);
			feeMonthService.init(monthInit);
			message = "初始化月份成功";
			addMessage(redirectAttributes, message);
			return "redirect:"+Global.getAdminPath()+"/business/feeMonth/list";
		}
		addMessage(model, message);
		return "modules/business/monthInitForm";
	}

}